package com.lxf.clouduser.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.lxf.clouduser.bean.UserRatingBean;
import com.lxf.clouduser.mapper.UserRatingItemRepository;
import com.lxf.clouduser.mapper.UserRatingMapper;
import com.lxf.clouduser.service.UserRatingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 罗兴发
 * @data 2025/7/4 15:07
 * @description：用户评分-业务实现层
 */
@Service
@Slf4j
public class UserRatingServiceImpl implements UserRatingService {

    @Resource
    UserRatingMapper userRatingMapper;
    @Resource
    private UserRatingItemRepository userRatingItemRepository;

    /**
     * 添加用户评分
     * @param json
     * @return
     */
    @Override
    public String addUserRating(JSONObject json) {
        JSONObject js = new JSONObject();

        // 获取参数
        Integer userId = json.getInteger("user_id");
        String objId = json.getString("obj_id");
        Integer objType = json.getInteger("obj_type");
        //用户取消收藏等操作，需要将分数减一，所以此参数需要计算好传递过来
        Integer userRating = json.getInteger("user_rating");

        log.info("获取数据信息Json：{}",json);
        log.info("获取数据信息userId：{}",userId);
        log.info("获取数据信息objId：{}",objId);
        log.info("获取数据信息objType：{}",objType);
        log.info("获取数据信息userRating：{}",userRating);

        // 根据对象类型、对象ID以及用户ID查询是否已作评分,若无则新增，若有则更新
        UserRatingBean rating = userRatingMapper.getRatingByObjIdAndObjTypeAndUserId(objId, objType, userId);
        if (null!=rating){//更新评分
            log.info("数据对象为：{}",rating);

            //重新设置分数
            rating.setUserRating(userRating);

            //更新操作
            int flag = userRatingMapper.updateUserRating(rating);
            if (flag<0){

                log.info("添加数据失败");
                js.put("code",401);
                js.put("msg","Fails");
            }else {
                js.put("code",200);
                js.put("msg","Success");

                userRatingItemRepository.save(rating);
                log.info("添加数据成功");
            }

        }else {//新增

            //list封装
            List<UserRatingBean> list = new ArrayList<>();

            //对象实例化
            UserRatingBean ur = new UserRatingBean();
            ur.setObjId(objId);
            ur.setObjType(objType);
            ur.setUserId(userId);
            ur.setUserRating(1);

            list.add(ur);

            log.info("打分对象：{}",ur);
            //添加操作
            int flag = userRatingMapper.addUserRating(list);
            if (flag<0){
                js.put("code",401);
                js.put("msg","Fails");
            }else {
                js.put("code",200);
                js.put("msg","Success");

                userRatingItemRepository.save(ur);
            }
        }

        return JSONObject.toJSONString(js, SerializerFeature.WriteMapNullValue);
    }

    @Override
    public int getUserRatingById(JSONObject json) {
        // 获取参数
        Integer userId = json.getInteger("user_id");
        String objId = json.getString("obj_id");
        Integer objType = json.getInteger("obj_type");
        // 根据对象类型、对象ID以及用户ID查询是否已作评分,若无则新增，若有则更新
        UserRatingBean rating = userRatingMapper.getRatingByObjIdAndObjTypeAndUserId(objId, objType, userId);
        if (null!=rating){
            return rating.getUserRating();
        }
        return 0;
    }

    @Override
    public int delUserRating() {
        userRatingItemRepository.deleteAll();
        return 1;
    }

    /**
     * 根据1类型查询分数
     * @param json
     * @return
     */
    @Override
    public String getUserRatingByObjType(JSONObject json) {
        JSONObject js = new JSONObject();
        Integer objType = json.getInteger("obj_type");
        List<UserRatingBean> list = userRatingMapper.findByObjType(objType);

        js.put("data",list);
        js.put("code",200);
        js.put("msg","Success");

        return JSONObject.toJSONString(js, SerializerFeature.WriteMapNullValue);
    }
}
